﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;
using ExitGames.Logging;
using ExitGames.Logging.Log4Net;
using System.IO;
using log4net.Config;
using MyGameServer.Manager;
using Common;
using MyGameServer.Handler;
using Common.Model;

namespace MyGameServer
{
    public class MyGameServer : ApplicationBase
    {
        public static readonly ILogger log = LogManager.GetCurrentClassLogger();
        public static MyGameServer Instance
        {
            get;
            private set;
        }

        public Dictionary<OperationCode, BaseHandler> HandlerDict = new Dictionary<OperationCode, BaseHandler>();
        public List<ClientPeer> peerList = new List<ClientPeer>();

        public List<Room> roomList = new List<Room>();
        public List<CommonUser> onlineUserList = new List<CommonUser>();

        //当一个客户端请求连接的时候
        protected override PeerBase CreatePeer(InitRequest initRequest)
        {
            ClientPeer peer = new ClientPeer(initRequest);
            peerList.Add(peer);
            return peer;
        }

        //初始化
        protected override void Setup()
        {
            Instance = this;


            log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(
                Path.Combine(this.ApplicationRootPath, "bin_Win64"), "log");

            FileInfo configFileInfo = new FileInfo(Path.Combine(this.BinaryPath, "log4net.config"));
            if(configFileInfo.Exists)
            {
                LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);
                XmlConfigurator.ConfigureAndWatch(configFileInfo);
            }

            log.Info("Setup Completed456!");
            
            ////////////////////////////
            IUserManager userManager = new UserManager();
            bool isUser1 = userManager.VerifyUser("1", "1");
            bool isUser2 = userManager.VerifyUser("2", "3");
            log.Info("isUser1: " + isUser1);
            log.Info("isUser2: " + isUser2);
            

            for (int i = 0; i < 4; i++)
            {
                Room room = new Room();
                room.roomId = 100000 + roomList.Count + 1;
                roomList.Add(room);
            }
            

            InitHandler();
        }

        public void InitHandler()
        {
            LoginHandler loginHandler = new LoginHandler();
            HandlerDict.Add(loginHandler.OpCode, loginHandler);

            DefaultHandler defaultHandler = new DefaultHandler();
            HandlerDict.Add(defaultHandler.OpCode, defaultHandler);

            RegisterHandler registerHandler = new RegisterHandler();
            HandlerDict.Add(registerHandler.OpCode, registerHandler);

            SyncPositionHandler syncPositionHandler = new SyncPositionHandler();
            HandlerDict.Add(syncPositionHandler.OpCode, syncPositionHandler);

            SyncPlayerHandler syncPlayerHandler = new SyncPlayerHandler();
            HandlerDict.Add(syncPlayerHandler.OpCode, syncPlayerHandler);

            ChatHandler chatHandler = new ChatHandler();
            HandlerDict.Add(chatHandler.OpCode, chatHandler);

            EmojiHandler emojiHandler = new EmojiHandler();
            HandlerDict.Add(emojiHandler.OpCode, emojiHandler);

            CreateRoomHandler createRoomHandler = new CreateRoomHandler();
            HandlerDict.Add(createRoomHandler.OpCode, createRoomHandler);

            JoinRoomHandler joinRoomHandler = new JoinRoomHandler();
            HandlerDict.Add(joinRoomHandler.OpCode, joinRoomHandler);

            ExitRoomHandler exitRoomHandler = new ExitRoomHandler();
            HandlerDict.Add(exitRoomHandler.OpCode, exitRoomHandler);

            ReadyHandler readyHandler = new ReadyHandler();
            HandlerDict.Add(readyHandler.OpCode, readyHandler);

            StartGameHandler startGameHandler = new StartGameHandler();
            HandlerDict.Add(startGameHandler.OpCode, startGameHandler);

            RoomListHandler roomListHandler = new RoomListHandler();
            HandlerDict.Add(roomListHandler.OpCode, roomListHandler);

            ThrowItemHandler throwItemHandler = new ThrowItemHandler();
            HandlerDict.Add(throwItemHandler.OpCode, throwItemHandler);

            
        }

        //server关闭的时候
        protected override void TearDown()
        {
            log.Info("服务器应用关闭了");
        }
    }
}
